/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Conexiones;

import control.Proveedor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author WILFREDO
 */
public class ProveedorBD {
    ConexionMySQL conexion;
    public ProveedorBD()
    {
        conexion = new ConexionMySQL("laboratorio","root","");
    }
    public boolean InsercionProveedor (Proveedor nuevo)  
    {   
        try {
            PreparedStatement psInsertar = null;
            String consultaInsert="INSERT INTO proveedores VALUES (?,?,?,?,?,?,?,?)";
            psInsertar = conexion.getConnection().prepareStatement(consultaInsert);
            psInsertar.setInt(1,nuevo.getProId());
            psInsertar.setString(2,nuevo.getProNom());
            psInsertar.setString(3,nuevo.getProRuc());
            psInsertar.setString(4,nuevo.getProNomEmp());
            psInsertar.setString(5,nuevo.getProApeEmp());
            psInsertar.setString(6,nuevo.getProDir());
            psInsertar.setString(7,nuevo.getProFon());
            psInsertar.setString(8,nuevo.getTipo());
            psInsertar.executeUpdate();
        } catch (SQLException ex) {
           return false;
        }
        return true;
            
    }
    
    
    public boolean ModificarProveedor (Proveedor nuevo)  
    {   
        try {
            PreparedStatement psModificar = null;
            String consultaUpdate="update proveedores set nombrePro=?,rucPro=?,enombresPro=?,eapellidosPro=?,direcPro=?,fonoPro=?    where idPro = ? ";
            psModificar = conexion.getConnection().prepareStatement(consultaUpdate);
            psModificar.setString(1,nuevo.getProNom());
            psModificar.setString(2,nuevo.getProRuc());
            psModificar.setString(3,nuevo.getProNomEmp());
            psModificar.setString(4,nuevo.getProApeEmp());
            psModificar.setString(5,nuevo.getProDir());
            psModificar.setString(6,nuevo.getProFon());
            psModificar.setInt(7,nuevo.getProId());
            psModificar.executeUpdate();
        } catch (SQLException ex) {
           return false;
        }
        return true;
            
    }
    
    
    public boolean EliminacionProveedor (int codigo)  
    {   
        try {
            PreparedStatement psEliminar = null;
            String consultaEliminar="delete from proveedores where idpro="+codigo;
            psEliminar = conexion.getConnection().prepareStatement(consultaEliminar);
            psEliminar.executeUpdate();
        } catch (SQLException ex) {
           return false;
        }
        return true;
            
    }

    
    
    public ArrayList<Proveedor> listaProveedores()
    {       
        ArrayList<Proveedor> lista= new ArrayList<Proveedor>();
      try   {
                ResultSet rs = null;
                PreparedStatement psConsultar = null;
                String consultaBuscar="select * from proveedores ";
                psConsultar = conexion.getConnection().prepareStatement(consultaBuscar);
                rs= psConsultar.executeQuery();
                while (rs.next()) 
                {
                    lista.add(new Proveedor(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7)));
                }
            
            
        } catch (SQLException ex) {
           
        }
      return lista;
    }
        
    public Proveedor BuscarProveedor (int codigo) 
        { 
            int ProId=0;
            String ProNom=null;
            String ProRuc=null;
            String ProNomEmp=null;
            String ProApeEmp=null;
            String ProDir=null;
            String ProFon=null;
        try {
            ResultSet rs = null;
            
            PreparedStatement psConsultar = null;
            String consultaBuscar="select * from proveedores where idPro="+codigo;
            psConsultar = conexion.getConnection().prepareStatement(consultaBuscar);
            rs= psConsultar.executeQuery();
            while (rs.next()) 
            {
                ProId = rs.getInt(1);
                ProNom= rs.getString(2);
                ProRuc=rs.getString(3);
                ProNomEmp=rs.getString(4);
                ProApeEmp=rs.getString(5);
                ProDir=rs.getString(6);
                ProFon=rs.getString(7);
                return new Proveedor(ProId,ProNom,ProRuc,ProNomEmp,ProApeEmp,ProDir,ProFon);
             }
            
            //return nombre+" "+fecnac;
        } catch (SQLException ex) {
           
        }
        
          return null;
        }
    
    
    public int BuscarProveedorCodigo (String nombre) 
        { 
            int ProId=0;
            
        try {
            ResultSet rs = null;
            
            PreparedStatement psConsultar = null;
            String consultaBuscar="select idPro from proveedores where nombrePro = ?";
            psConsultar = conexion.getConnection().prepareStatement(consultaBuscar);
            rs= psConsultar.executeQuery();
            while (rs.next()) 
            {
                ProId = rs.getInt(1);
                
             }
            
            //return nombre+" "+fecnac;
        } catch (SQLException ex) {
           
        }
        
          return ProId;
        }
    
    
     public int ConseguirCodigoProveedor () 
        {  
        
        int codigo =-1;
        try {
            ResultSet rs = null;
            
            
            PreparedStatement psConsultar = null;
            //String consultaBuscar="select count(idPro) from proveedores";
            String consultaBuscar="select max(idPro) from proveedores";
            
            psConsultar = conexion.getConnection().prepareStatement(consultaBuscar);
            rs= psConsultar.executeQuery();
            if (rs.next()) 
                codigo = rs.getInt(1);
            
        } catch (SQLException ex) {  System.out.print("error");      }
        return codigo+1;  
        }
     public static void main (String []args)
     {
            ProveedorBD proveedor= new ProveedorBD();
            proveedor.ConseguirCodigoProveedor();
     }
    
    
    
}
